<?php

/**
 * pedido actions.
 *
 * @package    zing
 * @subpackage pedido
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pedidoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
//    $this->pedidos = Doctrine_Core::getTable('Pedido')
//      ->createQuery('a')
//      ->execute();
    
        $this->linkFecha = strtr($request->getUri(),array('index'=>'indexFecha'));
        //die($this->linkFecha);
        
        
        $this->getContext()->getInstance()->getConfiguration()->loadHelpers("Url"); 


        $table = new sfTable(array('name'=>'tPedido','model'=>'pedido'),array('class'=>'pedidos')); //border:1px solid;',
        
//        $table->addFixedParameter('A', $request->getParameter('A',4));
//        
//        $table->addFixedParameter('B', $request->getParameter('B',7));
        
        $table->thSchema->setDefaultSort('fechaEntrega');
        $table->thSchema->setDefaultSortDirection('desc');         
        
        $table->tdSchema->setOption('defaultTdAttributes',array('style'=>"text-align: center;width: 84px; overflow:hidden;margin-left:3px"));         
        //------
        

        //------
        $table->thSchema['id'] = new sfTh(array('text'=>'id'));
        $table->thSchema['id']->setHidden()
                                ->setFilterType('number')
                                ->setFilterable(true,'Nro. Pedido');

        
        $table->tdSchema['id'] = new sfTdModel(array('field'=>'id'));         
        $table->tdSchema['id']->setHidden();

        //------        
        //------        
        $table->thSchema['fechaEntrega'] = new sfTh(array('text'=>'Fecha Entrega'));
        $table->thSchema['fechaEntrega']->setFilterable()       
                                        ->setFilterType('dateRange')
                                        ->setOption('filterGroup','Filtro Fechas');

        $table->tdSchema['fechaEntrega'] = new sfTdModelDate(array('field'=>'fechaEntrega','dateFormat'=>'d-m-y h:i:s'));                

        //------        
        $table->thSchema['Cliente'] = new sfTh(array('text'=>'Cliente'));    
        $table->thSchema['Cliente']->setFilterable(true,'Razon Social Cliente')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        
        $table->tdSchema['Cliente'] = new sfTdModel(array('field'=>'Cliente.razonSocial'));               

        
        //------        
        $table->thSchema['fechaCreacion'] = new sfTh(array('text'=>'Fecha Creacion'));
        $table->thSchema['fechaCreacion']->setFilterable()       
                                        ->setFilterType('dateRange')
                                        ->setOption('filterGroup','Filtro Fechas');

        $table->tdSchema['fechaCreacion'] = new sfTdModelDate(array('field'=>'created_at','dateFormat'=>'d/m/y'));                


        //------        
        $table->thSchema['pagado'] = new sfTh(array('text'=>'Pagado','sortable'=>false),array('style' => 'text-align: center;'));        
        
        $table->tdSchema['pagado'] = new  sfTdConditionalTemplate();

        
        //----
        $table->thSchema['entregado'] = new sfTh(array('text'=>'Entregado','sortable'=>false),array('style' => 'text-align: center;'));        
        
        $table->tdSchema['entregado'] = new  sfTdConditionalTemplate();
        
        
        
        //---
        
        
        $table->thSchema['entregadoVar'] = new sfTh(array('text'=>'Entregado'));
        $table->thSchema['entregadoVar']->setHidden()
                                        ->setFilterable()
                                        ->setFilterType('mark')
                                        ->setOption('filterGroup','Filtro Estados');
        $table->tdSchema['entregadoVar'] = new sfTdModel(array('field'=>'entregado'));      
        $table->tdSchema['entregadoVar']->setHidden();

        
        //---
        
        
        $table->thSchema['porcentajeRealizacion'] = new sfTh(array('text'=>'%'));
        $table->thSchema['porcentajeRealizacion']->setFilterable(false);

        
        $table->tdSchema['porcentajeRealizacion'] = new sfTdModel(array('field'=>'porcentajeRealizacion'));      
        //$table->tdSchema['porcentajeRealizacion']->setHidden();

        //------        
        
        
        $table->thSchema['pagadoVar'] = new sfTh(array('text'=>'Pagado'));        
        $table->thSchema['pagadoVar']->setHidden()
                                    ->setFilterable()
                                    ->setFilterType('mark')
                                    ->setOption('filterGroup','Filtro Estados');
        
        
        $table->tdSchema['pagadoVar'] = new sfTdModel(array('field'=>'pagado'));              
        $table->tdSchema['pagadoVar']->setHidden();
        
        //------               

        
        $table->thSchema['herramientas'] = new sfTh(array('text'=>'Herramientas','sortable'=>false),array('style' => 'text-align: center; width:50px;'));                
        //------                                    
        //$table->setFooterContent('Esto es el footer');
        
        $table->setPageSize(15);
        
        
        
        $table->setAction('pedido/index');

        $table->buildQuery($request);        
        $table->execute($request);        
        
        $this->tabla =  $table;
    
  }

  
  
  public function executeIndexFecha(sfWebRequest $request)
  {
//    $this->pedidos = Doctrine_Core::getTable('Pedido')
//      ->createQuery('a')
//      ->execute();
    
      
        $this->linkPorcentaje = strtr($request->getUri(),array('indexFecha'=>'index'));
        
        
        $this->getContext()->getInstance()->getConfiguration()->loadHelpers("Url"); 


        $table = new sfTable(array('name'=>'tPedido','model'=>'pedido'),array('class'=>'pedidos')); //border:1px solid;',
        
//        $table->addFixedParameter('A', $request->getParameter('A',4));
//        
//        $table->addFixedParameter('B', $request->getParameter('B',7));
        
        $table->thSchema->setDefaultSort('fechaEntrega');
        $table->thSchema->setDefaultSortDirection('desc');         
        
        $table->tdSchema->setOption('defaultTdAttributes',array('style'=>"text-align: center;width: 84px; overflow:hidden;margin-left:3px"));         
        //------
        

        //------
        $table->thSchema['id'] = new sfTh(array('text'=>'id'));
        $table->thSchema['id']->setHidden()
                                ->setFilterType('number')
                                ->setFilterable(true,'Nro. Pedido');

        
        $table->tdSchema['id'] = new sfTdModel(array('field'=>'id'));         
        $table->tdSchema['id']->setHidden();

        //------        
        //------        
        $table->thSchema['fechaEntrega'] = new sfTh(array('text'=>'Fecha Entrega'));
        $table->thSchema['fechaEntrega']->setFilterable()       
                                        ->setFilterType('dateRange')
                                        ->setOption('filterGroup','Filtro Fechas');

        $table->tdSchema['fechaEntrega'] = new sfTdModelDate(array('field'=>'fechaEntrega','dateFormat'=>'d-m-y h:i:s'));                

        //------        
        $table->thSchema['Cliente'] = new sfTh(array('text'=>'Cliente'));    
        $table->thSchema['Cliente']->setFilterable(true,'Razon Social Cliente')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        
        $table->tdSchema['Cliente'] = new sfTdModel(array('field'=>'Cliente.razonSocial'));               

        
        //------        
        $table->thSchema['fechaCreacion'] = new sfTh(array('text'=>'Fecha Creacion'));
        $table->thSchema['fechaCreacion']->setFilterable()       
                                        ->setFilterType('dateRange')
                                        ->setOption('filterGroup','Filtro Fechas');

        $table->tdSchema['fechaCreacion'] = new sfTdModelDate(array('field'=>'created_at','dateFormat'=>'d/m/y'));                


        //------        
        $table->thSchema['pagado'] = new sfTh(array('text'=>'Pagado','sortable'=>false),array('style' => 'text-align: center;'));        
        
        $table->tdSchema['pagado'] = new  sfTdConditionalTemplate();

        
        //----
        $table->thSchema['entregado'] = new sfTh(array('text'=>'Entregado','sortable'=>false),array('style' => 'text-align: center;'));        
        
        $table->tdSchema['entregado'] = new  sfTdConditionalTemplate();
        
        
        
        //---
        
        
        $table->thSchema['entregadoVar'] = new sfTh(array('text'=>'Entregado'));
        $table->thSchema['entregadoVar']->setHidden()
                                        ->setFilterable()
                                        ->setFilterType('mark')
                                        ->setOption('filterGroup','Filtro Estados');
        $table->tdSchema['entregadoVar'] = new sfTdModel(array('field'=>'entregado'));      
        $table->tdSchema['entregadoVar']->setHidden();

        
        //---
        
        
        $table->thSchema['porcentajeRealizacion'] = new sfTh(array('text'=>'%'));
        $table->thSchema['porcentajeRealizacion']->setFilterable(false);

        
        $table->tdSchema['porcentajeRealizacion'] = new sfTdModel(array('field'=>'porcentajeRealizacion'));      
        //$table->tdSchema['porcentajeRealizacion']->setHidden();

        //------        
        
        
        $table->thSchema['pagadoVar'] = new sfTh(array('text'=>'Pagado'));        
        $table->thSchema['pagadoVar']->setHidden()
                                    ->setFilterable()
                                    ->setFilterType('mark')
                                    ->setOption('filterGroup','Filtro Estados');
        
        
        $table->tdSchema['pagadoVar'] = new sfTdModel(array('field'=>'pagado'));              
        $table->tdSchema['pagadoVar']->setHidden();
        
        //------               

        
        $table->thSchema['herramientas'] = new sfTh(array('text'=>'Herramientas','sortable'=>false),array('style' => 'text-align: center; width:50px;'));                
        //------                                    
        //$table->setFooterContent('Esto es el footer');
        
        $table->setPageSize(15);
        
        
        
        $table->setAction('pedido/index');

        $table->buildQuery($request);        
        $table->execute($request);        
        
        $this->tabla =  $table;
    
  }
  
  
  
  
  
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PedidoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PedidoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($pedido = Doctrine_Core::getTable('Pedido')->find(array($request->getParameter('id'))), sprintf('Object pedido does not exist (%s).', $request->getParameter('id')));
    $this->form = new PedidoForm($pedido);
  }

  
  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($pedido = Doctrine_Core::getTable('Pedido')->find(array($request->getParameter('id'))), sprintf('Object pedido does not exist (%s).', $request->getParameter('id')));
    $this->pedido = $pedido;
  }
  
  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pedido = Doctrine_Core::getTable('Pedido')->find(array($request->getParameter('id'))), sprintf('Object pedido does not exist (%s).', $request->getParameter('id')));
    $this->form = new PedidoForm($pedido);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {

    $this->forward404Unless($pedido = Doctrine_Core::getTable('Pedido')->find(array($request->getParameter('id'))), sprintf('Object pedido does not exist (%s).', $request->getParameter('id')));
    $pedido->delete();

    $this->redirect('pedido/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pedido = $form->save();

      $this->redirect('pedido/show?id='.$pedido->getId());
    }
  }
}
